﻿using LuShanTea.DTO;
using LuShanTea.IService;
using LuShanTea.Service.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LuShanTea.Service
{
    public class RoleService : IRoleService
    {
        /// <summary>
        /// 添加角色
        /// </summary>
        /// <param name="name">角色名称</param>
        /// <returns></returns>
        public long AddNew(string name)
        {
            using (LuShanTeaContext context = new LuShanTeaContext())
            {
                BaseService<Role> baseService = new BaseService<Role>(context);

                bool isExists = baseService.GetAll().Any(r => r.Name == name);

                if (isExists)
                {
                    throw new ArgumentException("角色名称：" + name + "已存在");
                }

                Role role = new Role()
                {
                    Name = name
                };

                context.Roles.Add(role);
                context.SaveChanges();

                return role.Id;
            }
        }

        /// <summary>
        /// 给管理员添加角色
        /// </summary>
        /// <param name="adminUserId">管理员Id</param>
        /// <param name="roleIds">角色id</param>
        public void AddRole(long adminUserId, long[] roleIds)
        {
            using (LuShanTeaContext context = new LuShanTeaContext())
            {

                BaseService<AdminUser> adminUserBaseService = new BaseService<AdminUser>(context);
                AdminUser adminUser = adminUserBaseService.GetById(adminUserId);

                if (adminUser == null)
                {
                    throw new ArgumentException("用户Id为：" + adminUserId + "不存在");
                }

                BaseService<Role> roleBaseService = new BaseService<Role>(context);
                var roles = roleBaseService.GetAll()
                    .Where(r => roleIds.Contains(r.Id));

                foreach (var role in roles)
                {
                    adminUser.Roles.Add(role);
                }

                context.SaveChanges();
            }
        }

        /// <summary>
        /// 获取所有角色
        /// </summary>
        /// <returns></returns>
        public RoleDTO[] GetAll()
        {
            using (LuShanTeaContext context = new LuShanTeaContext())
            {
                BaseService<Role> baseService = new BaseService<Role>(context);

                return baseService.GetAll().ToList()
                    .Select(r => ToDTO(r)).ToArray();
            }
        }

        /// <summary>
        /// 获取管理的所有角色
        /// </summary>
        /// <param name="adminUserId">管理员Id</param>
        /// <returns></returns>
        public RoleDTO[] GetByAdminUserId(long adminUserId)
        {
            using (LuShanTeaContext context = new LuShanTeaContext())
            {

                BaseService<AdminUser> adminUserBaseService = new BaseService<AdminUser>(context);
                AdminUser adminUser = adminUserBaseService.GetById(adminUserId);

                if (adminUser == null)
                {
                    throw new ArgumentException("管理员Id为：" + adminUserId + "不存在");
                }

                return adminUser.Roles.ToList()
                    .Select(r => ToDTO(r)).ToArray();

            }
        }

        /// <summary>
        /// 根据角色Id获取角色
        /// </summary>
        /// <param name="roleId">角色id</param>
        /// <returns></returns>
        public RoleDTO GetById(long roleId)
        {
            using (LuShanTeaContext context = new LuShanTeaContext())
            {
                BaseService<Role> baseService = new BaseService<Role>(context);

                var role = baseService.GetById(roleId);
                return role == null ? null : ToDTO(role);
            }
        }


        /// <summary>
        /// 根据角色名称获取角色
        /// </summary>
        /// <param name="roleName">角色名称</param>
        /// <returns></returns>
        public RoleDTO GetByName(string roleName)
        {
            using (LuShanTeaContext context = new LuShanTeaContext())
            {
                BaseService<Role> baseService = new BaseService<Role>(context);

                var role = baseService.GetAll().SingleOrDefault(r => r.Name == roleName);
                return role == null ? null : ToDTO(role);
            }
        }

        /// <summary>
        /// 软删除数据
        /// </summary>
        /// <param name="roleId"></param>
        public void MarkDeleted(long roleId)
        {
            using (LuShanTeaContext context = new LuShanTeaContext())
            {
                BaseService<Role> baseService = new BaseService<Role>(context);

                Role role = baseService.GetById(roleId);

                if (role == null)
                {
                    throw new ArgumentException(string.Format($"角色Id为：{roleId}不存在"));
                }

                baseService.MakeDelete(roleId);
            }
        }

        /// <summary>
        /// 更新角色信息
        /// </summary>
        /// <param name="roleId">角色id</param>
        /// <param name="name">名称</param>
        public void Update(long roleId, string name)
        {
            using (LuShanTeaContext context = new LuShanTeaContext())
            {
                BaseService<Role> baseService = new BaseService<Role>(context);

                Role role = baseService.GetById(roleId);

                if (role == null)
                {
                    throw new ArgumentException(string.Format($"角色Id为：{roleId}不存在"));
                }

                role.Name = name;

                context.SaveChanges();
            }
        }

        /// <summary>
        /// 更新管理员角色
        /// </summary>
        /// <param name="adminUserId">管理员id</param>
        /// <param name="roleIds">角色id</param>
        public void UpdateRoles(long adminUserId, long[] roleIds)
        {
            using (LuShanTeaContext context = new LuShanTeaContext())
            {

                BaseService<AdminUser> adminUserBaseService = new BaseService<AdminUser>(context);
                AdminUser adminUser = adminUserBaseService.GetById(adminUserId);

                if (adminUser == null)
                {
                    throw new ArgumentException("用户Id为：" + adminUserId + "不存在");
                }

                BaseService<Role> roleBaseService = new BaseService<Role>(context);
                var roles = roleBaseService.GetAll()
                    .Where(r => roleIds.Contains(r.Id));

                adminUser.Roles.Clear();

                foreach (var role in roles)
                {
                    adminUser.Roles.Add(role);
                }

                context.SaveChanges();
            }
        }

        private RoleDTO ToDTO(Role role)
        {
            return new RoleDTO
            {
                Id = role.Id,
                Name = role.Name,
                CreateDateTime = role.CreateDateTime
            };
        }
    }
}
